﻿#region Imports

using System;
using System.Web.UI.WebControls;
using d3PrtDgn.Data.IDAO;
using d3PrtDgn.Data.Domain.Entity;
using d3PrtDgn.Common.Common;

#endregion

namespace d3PrtDgn.Controls
{
	///<summary>
	/// User control for category list displaying and 
	/// redirecting to album list in selected category
	///</summary>
	public partial class CategoryList : UserControl
	{
		#region Private fileds

		private System.Web.UI.IHierarchicalEnumerable categoryDataSource;
		private ICategoryDao categoryDao;
		private Category selectedCategory;
        private Category rootCategory;

		#endregion

		#region Public properties

		/// <summary>
		/// Data source for category tree view
		/// </summary>
		public System.Web.UI.IHierarchicalEnumerable CategoryDataSource
		{
			set { categoryDataSource = value; }
		}

		/// <summary>
		/// Sets cateogory DAO
		/// </summary>
		public ICategoryDao CategoryDao
		{
			set { categoryDao = value; }
		}

		/// <summary>
		/// Gets currently selected category
		/// </summary>
		public Category SelectedCategory
		{
			get { return selectedCategory; }
		}

        /// <summary>
        /// Sets root category of subtree to display
        /// </summary>
        public Category RootCategory
        {
            set { rootCategory = value; }
        }

		#endregion

		#region Event handlers

		/// <summary>
		/// Handles page load event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				InitCategories();
			}
		}

		/// <summary>
		/// Handles category change event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void ShowCategory(object sender, EventArgs e)
		{
			int id = Convert.ToInt32(((TreeView)sender).SelectedValue);
			selectedCategory = categoryDao.FindById(id);

			SetResult("Gallery");
		}

		#endregion

		#region Private methods

		/// <summary>
		/// Initializes category tree
		/// </summary>
		private void InitCategories()
		{
			tvCategories.DataSource = categoryDataSource;
			DataBind();
		}

		/// <summary>
		/// Binds data
		/// </summary>
		public new void DataBind()
		{
			tvCategories.DataBind();
		}

		#endregion
	}
}